package puyu.manage.service.source.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import puyu.manage.service.common.dao.CommonMapper;
import puyu.manage.service.source.dao.SysTSourceListMapper;
import puyu.manage.service.source.dao.SysTSourceMapper;
import puyu.manage.service.source.entity.SysTSource;
import puyu.manage.service.source.entity.SysTSourceList;
import puyu.manage.service.source.service.SysTSourceService;
import org.springframework.stereotype.Service;
import puyu.manage.service.utils.IdUtils;
import puyu.manage.service.utils.StringUtils;
import puyu.manage.service.utils.except.CustomException;
import puyu.manage.service.utils.tree.Zone;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典主表(SysTSource)表服务实现类
 *
 * @author makejava
 * @since 2024-08-01 16:41:15
 */
@Service
public class SysTSourceServiceImpl extends ServiceImpl<SysTSourceMapper, SysTSource> implements SysTSourceService {

    @Resource
    private SysTSourceMapper sysTSourceMapper;

    @Resource
    private SysTSourceListService sysTSourceListService;

    @Resource
    private SysTSourceListMapper sysTSourceListMapper;

    @Resource
    private CommonMapper commonMapper;

    @Override
    public List<Map<String, Object>> selectAll(Map<String, Object> source) {
        List<Map<String, Object>> list = sysTSourceMapper.selectList(source);
        return list;
    }

    /**
     * 新增
     * @param source
     */
    @Override
    public void insert(Map<String, Object> source) {
        //code关联子表键值不允许冲重复
        String code = (String) source.get("code");
        if (StringUtils.isEmpty(code)){
            throw new CustomException("编码不允许为空！");
        }
        SysTSource sysTSource = new SysTSource();
        sysTSource.setCode(code);
        List<SysTSource> sysTSources = sysTSourceMapper.selectAll(sysTSource);

        if (ObjectUtil.isNotEmpty(sysTSources)){
            throw new CustomException("编码重复！");
        }
        source.put("sourceId", IdUtils.simpleUUID());
        source.put("sourcetype", "1");
        sysTSourceMapper.insertSource(source);

    }

    /**
     * 修改
     */
    @Override
    public void update(Map<String, Object> source) {
        //code关联子表键值不允许冲重复
        String code = (String) source.get("code");
        if (StringUtils.isEmpty(code)){
            throw new CustomException("编码不允许为空！");
        }
        String sourceId = (String) source.get("sourceId");

        //判断编码是否重复
        List<Map<String,Object>> sysTSources = sysTSourceMapper.setlectBYSource(source);

        if (ObjectUtil.isNotEmpty(sysTSources)){
            throw new CustomException("编码重复！");
        }

        SysTSource byId = this.getById(sourceId);

        //如果修改编码，同步修改子表编码
        SysTSourceList list = new SysTSourceList();
        list.setSourceCode(byId.getCode());
        List<SysTSourceList> sysTSourceLists = sysTSourceListMapper.selectAll(list);

        if (ObjectUtil.isNotEmpty(sysTSourceLists)){
            for (SysTSourceList sysTSourceList : sysTSourceLists) {
                sysTSourceList.setSourceCode(code);
            }
            sysTSourceListService.updateBatchById(sysTSourceLists);
        }

        //修改
        sysTSourceMapper.updateSource(source);


    }

    /**
     * 批量删除
     * @param sourceIds
     */
    @Override
    public void deleteBySourceIds(List<String> sourceIds) {

        if (ObjectUtil.isNotEmpty(sourceIds)){
            //删除子表
            for (String id:sourceIds) {
                commonMapper.executeSql("delete from sys_t_source_list where source_code in (SELECT code from sys_t_source where source_id='"+id+"')");
            }
            //删除主表
            this.removeByIds(sourceIds);



        }

    }

    /**
     * 通过主表编码查询子表
     **/
    @Override
    public List<Map<String,Object>> getBySourceCode(Map<String,Object> source) {
        String sourceCode = (String) source.get("sourceCode");
        if (StringUtils.isEmpty(sourceCode)){
            throw new CustomException("主表编码为空！");
        }
        List<Map<String,Object>> list = sysTSourceListMapper.getBySourceCode(source);
        return list;
    }




}

